package com.lz.config.handlerInterceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 这个是SpringMvc的拦截器配置类，它用于在请求处理前后执行逻辑。它通常用于实现身份验证、日志记录、权限检查等功能。
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 配置允许跨域的路径
                .allowedOrigins("*") // 允许所有前端域名和端口
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的HTTP方法
                .allowedHeaders(".*") // 允许所有请求头
                .allowCredentials(true); // 允许携带Cookie
    }


    @Resource
    private LogInterceptor logInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logInterceptor)
                .addPathPatterns("/**"); // 指定拦截的路径
    }
}